package com.ttmall.controller;

import com.ttmall.common.pojo.EasyUIDataGridResult;
import com.ttmall.common.pojo.EasyUITreeNode;
import com.ttmall.common.pojo.TaotaoResult;
import com.ttmall.pojo.TbItem;
import com.ttmall.service.ItemCatService;
import com.ttmall.service.ItemService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tianyi on 2017/12/30.
 */
@Controller
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private ItemService itemService;


    @RequestMapping("/{itemId}")
    @ResponseBody
    private TaotaoResult getItemById(@PathVariable Long itemId) {
        return itemService.getItemById(itemId);
    }

    @RequestMapping("/{itemId}/desc")
    @ResponseBody
    private TaotaoResult getItemDescById(@PathVariable Long itemId) {
        return itemService.getItemDescById(itemId);
    }

    @RequestMapping("/list")
    @ResponseBody
    private EasyUIDataGridResult getItemList(@RequestParam Integer page, @RequestParam Integer rows) {
        return itemService.getItemList(page, rows);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    private TaotaoResult createItem(TbItem tbItem, String desc, String itemParams) {
        return itemService.createItem(tbItem, desc, itemParams);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    private TaotaoResult updateItem(TbItem tbItem, String desc, Long itemParamId, String itemParams) {
        return itemService.updateItem(tbItem, desc, itemParamId, itemParams);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    private TaotaoResult deleteItem(String ids) {

        if (StringUtils.isNotEmpty(ids)) {
            String[] idSplit = ids.split(",");
            List<Long> idLongs = new ArrayList<>();
            for (String s : idSplit) {
                try {
                    idLongs.add(new Long(s));
                } catch (NumberFormatException ex) {
                    //do nothing
                }
            }
            if (idLongs.size() > 0) {
                return itemService.deleteItems(idLongs);
            }
        }
        return TaotaoResult.build(400, "error params!");
    }

    @RequestMapping(value = "/instock", method = RequestMethod.POST)
    @ResponseBody
    private TaotaoResult instockItem(String ids) {
        if (StringUtils.isNotEmpty(ids)) {
            String[] idSplit = ids.split(",");
            List<Long> idLongs = new ArrayList<>();
            for (String s : idSplit) {
                try {
                    idLongs.add(new Long(s));
                } catch (NumberFormatException ex) {
                    //do nothing
                }
            }
            if (idLongs.size() > 0) {
                return itemService.instockItems(idLongs);
            }
        }
        return TaotaoResult.build(400, "error params!");
    }

    @RequestMapping(value = "/reshelf", method = RequestMethod.POST)
    @ResponseBody
    private TaotaoResult reshelfItem(String ids) {
        if (StringUtils.isNotEmpty(ids)) {
            String[] idSplit = ids.split(",");
            List<Long> idLongs = new ArrayList<>();
            for (String s : idSplit) {
                try {
                    idLongs.add(new Long(s));
                } catch (NumberFormatException ex) {
                    //do nothing
                }
            }
            if (idLongs.size() > 0) {
                return itemService.reshelfItems(idLongs);
            }
        }
        return TaotaoResult.build(400, "error params!");
    }

}
